package cn.com.cpic.insurance.rulecheck.web;

import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import cn.com.cpic.insurance.rulecheck.entity.RuleDefinition;
import cn.com.cpic.insurance.rulecheck.service.RuleDefinitionService;

@Results( { @Result(name = CrudActionSupport.RELOAD, location = "rule-def.action", type = "redirect") })
public class RuleDefAction extends CrudActionSupport<RuleDefinition> {

	private static final long serialVersionUID = -5086505209066238265L;

	private RuleDefinitionService service;

	private Long id;

	private RuleDefinition entity;

	private List<RuleDefinition> items;

	public String execute() {
		return list();
	}

	public String list() {
		items = service.getRuleDefinitionList();
		return SUCCESS;
	}

	public String input() {
		prepareModel();
		return INPUT;
	}

	public String save() {
		service.saveRuleDefinition(this.entity);
		return RELOAD;
	}

	public String delete() {
		service.deleteRuleDefinition(this.id);
		return RELOAD;
	}

	protected void prepareModel() {
		if (id != null) {
			this.entity = service.getRuleDefinition(id);
		} else {
			this.entity = new RuleDefinition();
		}
	}

	@Autowired
	public void setService(RuleDefinitionService service) {
		this.service = service;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public List<RuleDefinition> getItems() {
		return items;
	}

	public RuleDefinition getModel() {
		return entity;
	}
}
